﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utils.Extensions
{
    public class TryExtensions
    {
        /// <summary>
        /// try-catch包装
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="func"></param>
        /// <returns></returns>
        /// <exception cref="RepositoryException"></exception>
        public static async Task<TResult> Trycatch<TResult>(Func<Task<TResult>> func)
        {
            try
            {
                return await func();
            }
            catch (Exception ex)
            {
                // 自定义异常处理，可以记录日志
                throw new RepositoryException("数据库操作发生错误", ex);
            }
        }
    }
}
